package com.buding.fx.service.impl;


import com.buding.fx.common.constant.CssStyleInfo;
import com.buding.fx.common.constant.MenuInfo;
import com.buding.fx.common.enums.PosEnum;
import com.buding.fx.entity.Menu;
import com.buding.fx.entity.SystemConfigInfo;
import com.buding.fx.service.BaseViewService;
import com.buding.fx.utils.NotifyThread;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


/**
 * @author mingdongwei-jwk
 * @date 2022/09/15 10:58
 * @class com.buding.fx.service
 * @Description
 */

public class TopPinaBuildService implements BaseViewService {
    Logger logger = LoggerFactory.getLogger(TopPinaBuildService.class);

    /**
     *
     * @param config
     * @return
     */

    @Override
    public Node show(SystemConfigInfo config) {
        /**
         * top 分为上下两部分，上下两部分都是水平布局，其中上面部分左边是logo，中间是xx系统艺术字体，右边是版本展示
         * 下面部分
         */
        CssStyleInfo css = config.getCss();
        VBox vBox = new VBox();
        vBox.setPrefWidth(css.getTop_wight());
        vBox.setPrefHeight(css.getTop_height());
        vBox.setStyle("-fx-background-color: white");
        Node top = getTopNode(css);
        Node foot = getFootNode(config);
        vBox.getChildren().addAll(top,foot);
        logger.info("top node init end, top width: "+vBox.getPrefWidth()+"; top height: "+vBox.getPrefHeight());
        return vBox;
    }

    @Override
    public Node onclickShow(Node onClickNode, SystemConfigInfo configInfo) {
        return null;
    }


    private Node  getTopNode(CssStyleInfo css){
        BorderPane borderPane = new BorderPane();
        borderPane.setPrefWidth(css.getTop_logo_line_width());
        borderPane.setPrefHeight(css.getTop_logo_line_height());

        double everyNodeWidth =  css.getTop_logo_line_width()/3;

        VBox logoVbox = new VBox();
        logoVbox.setPrefWidth(everyNodeWidth);
        logoVbox.setPrefHeight(borderPane.getHeight());
        logoVbox.setAlignment(Pos.CENTER_LEFT);

        Image image = new Image(css.getLogoUrl());
        ImageView imageView = new ImageView(image);
        logoVbox.getChildren().add(imageView);
        VBox.setMargin(imageView,new Insets(0,0,0,10));

        VBox titleBox = new VBox();
        titleBox.setPrefWidth(everyNodeWidth);
        titleBox.setPrefHeight(borderPane.getHeight());
        titleBox.setAlignment(Pos.CENTER);

        Text title = getTitleText(css);
        titleBox.getChildren().add(title);

        VBox versionBox = new VBox();
        versionBox.setPrefWidth(everyNodeWidth);
        versionBox.setPrefHeight(borderPane.getHeight());
        versionBox.setAlignment(Pos.CENTER_RIGHT);

        Text vesionText = getVersionText(css.getVersion());
        versionBox.getChildren().add(vesionText);
        VBox.setMargin(vesionText,new Insets(0,10,0,0));

        borderPane.setLeft(logoVbox);
        borderPane.setCenter(title);
        borderPane.setRight(versionBox);
        return borderPane;
    }


    private Node getFootNode(SystemConfigInfo config){
        CssStyleInfo css = config.getCss();
        String hstyle ="-fx-background-color: "+css.getNavigation_backhround_color();
        FlowPane flowPane = new FlowPane();
        flowPane.setPrefWidth(css.getTop_navigation_bar_width());
        flowPane.setPrefHeight(css.getTop_navigation_bar_height());
        flowPane.setStyle(hstyle);
        flowPane.setHgap(css.getIcon_gap());
        flowPane.setAlignment(Pos.CENTER_LEFT);
        List<Menu> menus = MenuInfo.menus;
        for (Menu menu: menus){
            if (menu.getLevel()==0){  //一级菜单放最上面
                String tile = menu.getTitle();
                GridPane gridPane = new GridPane();
                String iconPath = menu.getIconPath()+menu.getIcon();
                Image image = new Image(iconPath);
                ImageView imageView = new ImageView(image);
                double dftWight = css.getTop_icon_img_width();
                double oclickWight = css.getOnclick_icon_width();
                imageView.setFitWidth(dftWight);
                imageView.setFitHeight(dftWight);
                Label label = new Label(tile);
                String fontFamily = css.getNavigation_font_famliy();
                double fontSize = css.getNavigation_font_size();
                double onClickSize  = css.getOnclick_navigation_font_size();
                FontWeight fontWeight = FontWeight.EXTRA_BOLD;
                FontPosture fontPosture = FontPosture.ITALIC;
                Font tfont = Font.font(fontFamily, fontWeight , fontPosture, fontSize);
                label.setFont(tfont);
                Color gray = Color.web(css.getNavigation_font_color());
                Color white = Color.web(css.getNavigation_font_click_color());
                label.setTextFill(gray);
                gridPane.add(imageView,0,0);
                gridPane.add(label,1,0);
                flowPane.getChildren().add(gridPane);
                boolean isClick = false;
                gridPane.getProperties().put("isClick",false);
                gridPane.getProperties().put("menuId",menu.getId());
                //鼠标移入字体变白
                gridPane.setOnMouseEntered(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        label.setTextFill(white);
                    }
                });
                gridPane.setOnMouseExited(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        //需要判断当前是否是点击了以后移除，如果是，不能设置为灰色
                        Boolean isClick = (Boolean) gridPane.getProperties().get("isClick");
                        logger.info("mouse on exit，isClick: "+isClick);
                        if (!isClick){
                            label.setTextFill(gray);
                        }
                    }
                });

                //鼠标点击图片和字体大小变大，其余的变小
                gridPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        gridPane.getProperties().put("isClick",true);  //设置点击标识
                        imageView.setFitWidth(oclickWight);
                        imageView.setFitHeight(oclickWight);
                        Font nfnt = Font.font(fontFamily, fontWeight , fontPosture, onClickSize);
                        label.setFont(nfnt);
                        label.setTextFill(white);
                        //其它按钮都需要还原
                        ObservableList<Node> children = flowPane.getChildren();
                        for (Node node:children){
                            if (node!=gridPane){
                                GridPane gridPane = (GridPane) node;
                                ImageView imgv = (ImageView) gridPane.getChildren().get(0);
                                imgv.setFitWidth(dftWight);
                                imgv.setFitHeight(dftWight);

                                Label label = (Label) gridPane.getChildren().get(1);
                                Font tfont = Font.font(fontFamily, fontWeight , fontPosture, fontSize);
                                label.setFont(tfont);
                                label.setTextFill(gray);
                            }
                        }

                        //展示左面的树形结构
                        LeftPinaBuildService leftPinaBuildService = new LeftPinaBuildService();
                        leftPinaBuildService.onclickShow(gridPane,config);
                        //异步更新左边组件
                        Platform.runLater(new NotifyThread(config.getBorderPane(), leftPinaBuildService.onclickShow(gridPane,config), PosEnum.LEFT));
                    }
                });
            }
        }
        ObservableList<Node> children = flowPane.getChildren();
        Node node = children.get(0);
        FlowPane.setMargin(node,new Insets(0,0,0,20));

        //给第一个GridPane添加一个默认的鼠标点击事件
       /* MouseEvent mouseEvent = new MouseEvent(MouseEvent.MOUSE_CLICKED, 1,1,1,1, MouseButton.PRIMARY, 1,
                true, true, true, true,
                true, true, true,
                true, true, true, null);
        Event.fireEvent(node, mouseEvent);*/
        return flowPane;
    }

    private Text getTitleText(CssStyleInfo cssStyleInfo){
        DropShadow ds = new DropShadow();
        ds.setOffsetY(7.0f);
        ds.setColor(Color.web(cssStyleInfo.getSystem_name_font_shadow_color()));
        Text t = new Text();
        t.setText(cssStyleInfo.getSystemName());
        double fontSize = 30;
        FontWeight fontWeight = FontWeight.BLACK;
        FontPosture fontPosture = FontPosture.ITALIC;
        Font tfont = Font.font(cssStyleInfo.getSystem_name_font_family(), fontWeight , fontPosture, fontSize);
        t.setEffect(ds);
        t.setFont(tfont);
        t.setFill(Color.web(cssStyleInfo.getSystem_name_font_color()));
        return t;
    }

    private Text getVersionText(String content){
        Text t = new Text();
        t.setText("上午好，张三。 软件版本： "+content);
        String fontFamily = "Bauhaus 93";
        double fontSize = 12;
        FontWeight fontWeight = FontWeight.BOLD;
        FontPosture fontPosture = FontPosture.ITALIC;
        Font tfont = Font.font(fontFamily, fontWeight , fontPosture, fontSize);
        t.setFont(tfont);
        t.setFill(Color.gray(0.5));
        return t;
    }

}
